package org.NooLab.glue.net;

import java.util.Vector;


/**
 * 
 * the list of remote addresses of further, cascading MessageBoards
 * 
 * 
 */
public class BoardAddresses {

	Vector<BoardAddress> items = new Vector<BoardAddress>();
	
	
	public BoardAddresses(){
		
	}

	
	
	public int indexOf(String host){
		 
		return indexOf(host,0);
	}

	public int indexOf(String host, int kind){
		 
		return indexOf(host,-1,kind);
		  
	}
	
	/**
	 * 
	 * @param host
	 * @param port
	 * @param kind  0=all, 1=active,   in collection of all, 3=registering  11==active, in collection of active 
	 *                    -1=inactive, in collection of all, -11==inactive, in collection of inactive
	 * @return
	 */
	public int indexOf(String host, int port, int kind){
		int index=-1;
		boolean hb=false;
		BoardAddress boardaddress;
		Vector<BoardAddress> addresses = new Vector<BoardAddress>();
		
		if (kind>=0){
			if (kind == 2) {
				addresses = getActive();
			} else {
				addresses = items;
			}
		}
		if (kind<=0){
			if (kind == -2) {
				addresses = getinActive();
			} else {
				addresses = items;
			}
		}
		
		
		for(int i=0;i<addresses.size();i++){
			
			hb=false;
			boardaddress = addresses.get(i) ;
			
			if (kind >=0 ){
				if (boardaddress.isAvailable){
					if (boardaddress.address.hostname.contains(host)){
						hb = true;
					}
				}
			}
			if (kind <=0 ){
				
			}
			
			if (hb){
				if (port>0){
					if (boardaddress.address.port!=port){
						hb = false;
					}
				}
			}
			if (hb){
				index=i;
				break;
			}
		} // i->
		
		return index;
	}

	public int indexOf( BoardAddress boardaddress){
		int index=-1;
		
		return index;
	}
	
	public int indexOf( BoardAddress boardaddress, int kind){
		int index=-1;
		
		return index;
	}
	
	
	public BoardAddress get( int index){
		BoardAddress address = null;
		
		if ((index>=0) && (index<items.size())){
			address = items.get(index) ;
		}
		
		return address ;
	}
	
	
	public Vector<BoardAddress> getinActive(){
		Vector<BoardAddress> activeItems = new Vector<BoardAddress>();
		
		BoardAddress item;
		
		for (int i=0;i<items.size();i++){
			item = items.get(i) ;
			if (item.isActive==false){
				activeItems.add(item) ;
			}
		}
		
		return activeItems;
	}
	
	public Vector<BoardAddress> getActive(){
		
		Vector<BoardAddress> activeItems = new Vector<BoardAddress>();
		
		BoardAddress item;
		
		for (int i=0;i<items.size();i++){
			item = items.get(i) ;
			if (item.isActive){
				activeItems.add(item) ;
			}
		}
		
		return activeItems;
	}

	/**
	 * 
	 * 
	 * 
	 * @param statevalue
	 * @param mode  0=at least the requested state value, 1=exact the requested state value
	 * @return
	 */
	public int countByState( int statevalue, int mode){

		int result = 0, sv;
		BoardAddress item;
		boolean hb;
		
		
		for (int i=0;i<items.size();i++){
			item = items.get(i) ;
			sv = item.getState() ;
			hb = false;
			
			if (mode<=0){  // at least
				hb = sv>=statevalue ;
			}
			if (mode>=1){
				hb = sv==statevalue ;
			}
			if (hb){
				result++;
			}
		}
		
		return result;
	}
	
	public BoardAddress getByStateFirst( int statevalue, int mode){

		int result = 0, sv, ix;
		BoardAddress item=null;
		boolean hb;
		
		
		for (int i=0;i<items.size();i++){
			item = items.get(i) ;
			sv = item.getState() ;
			hb = false;
			
			if (mode<=0){  // at least
				hb = sv>=statevalue ;
			}
			if (mode>=1){
				hb = sv==statevalue ;
			}
			if (hb){
				
				break;
			}
		}
		
		return item;
	}
	
	public int countActive(){
		
		int result = 0;
		BoardAddress item;
		
		for (int i=0;i<items.size();i++){
			item = items.get(i) ;
			if (item.isActive){
				result++;
			}
		}
		
		return result;
	}

	public int size(){
		return items.size() ;
	}

	public void set( BoardAddress address){
		boolean dv=false;
		
		for (int i=0;i<items.size();i++){
			if (items.get(i).equals(address) ){
				dv = true;
				break;
			}
		}
		
		if (dv==false){
			items.add(address) ;
		}
		
	}

	public BoardAddress set( RemoteAddress raddress){
		boolean dv=false;
		BoardAddress address=null;
		
		
		for (int i=0;i<items.size();i++){
			if (items.get(i).equals(raddress) ){
				dv = true;
				address = items.get(i);
				break;
			}
		}
		
		if (dv==false){
			
			address = new BoardAddress() ;
			address.setAddress(raddress);
			items.add(address) ;
			address = items.get(items.size()-1);
		}
		return address;
	}
	
}
